package com.room.search.web.rest;

import com.room.search.repository.RoomUserPermissionRepository;
import com.room.search.service.RoomUserPermissionService;
import com.room.search.service.dto.RoomUserPermissionDTO;
import com.room.search.web.rest.errors.BadRequestAlertException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import tech.jhipster.web.util.HeaderUtil;
import tech.jhipster.web.util.ResponseUtil;

/**
 * REST controller for managing {@link com.room.search.domain.RoomUserPermission}.
 */
@RestController
@RequestMapping("/api")
public class RoomUserPermissionResource {

    private final Logger log = LoggerFactory.getLogger(RoomUserPermissionResource.class);

    private static final String ENTITY_NAME = "roomUserPermission";

    @Value("${jhipster.clientApp.name}")
    private String applicationName;

    private final RoomUserPermissionService roomUserPermissionService;

    private final RoomUserPermissionRepository roomUserPermissionRepository;

    public RoomUserPermissionResource(
        RoomUserPermissionService roomUserPermissionService,
        RoomUserPermissionRepository roomUserPermissionRepository
    ) {
        this.roomUserPermissionService = roomUserPermissionService;
        this.roomUserPermissionRepository = roomUserPermissionRepository;
    }

    /**
     * {@code POST  /room-user-permissions} : Create a new roomUserPermission.
     *
     * @param roomUserPermissionDTO the roomUserPermissionDTO to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new roomUserPermissionDTO, or with status {@code 400 (Bad Request)} if the roomUserPermission has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/room-user-permissions")
    public ResponseEntity<RoomUserPermissionDTO> createRoomUserPermission(@Valid @RequestBody RoomUserPermissionDTO roomUserPermissionDTO)
        throws URISyntaxException {
        log.debug("REST request to save RoomUserPermission : {}", roomUserPermissionDTO);
        if (roomUserPermissionDTO.getId() != null) {
            throw new BadRequestAlertException("A new roomUserPermission cannot already have an ID", ENTITY_NAME, "idexists");
        }
        RoomUserPermissionDTO result = roomUserPermissionService.save(roomUserPermissionDTO);
        return ResponseEntity
            .created(new URI("/api/room-user-permissions/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * {@code PUT  /room-user-permissions/:id} : Updates an existing roomUserPermission.
     *
     * @param id the id of the roomUserPermissionDTO to save.
     * @param roomUserPermissionDTO the roomUserPermissionDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomUserPermissionDTO,
     * or with status {@code 400 (Bad Request)} if the roomUserPermissionDTO is not valid,
     * or with status {@code 500 (Internal Server Error)} if the roomUserPermissionDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PutMapping("/room-user-permissions/{id}")
    public ResponseEntity<RoomUserPermissionDTO> updateRoomUserPermission(
        @PathVariable(value = "id", required = false) final Long id,
        @Valid @RequestBody RoomUserPermissionDTO roomUserPermissionDTO
    ) throws URISyntaxException {
        log.debug("REST request to update RoomUserPermission : {}, {}", id, roomUserPermissionDTO);
        if (roomUserPermissionDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomUserPermissionDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomUserPermissionRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        RoomUserPermissionDTO result = roomUserPermissionService.save(roomUserPermissionDTO);
        return ResponseEntity
            .ok()
            .headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomUserPermissionDTO.getId().toString()))
            .body(result);
    }

    /**
     * {@code PATCH  /room-user-permissions/:id} : Partial updates given fields of an existing roomUserPermission, field will ignore if it is null
     *
     * @param id the id of the roomUserPermissionDTO to save.
     * @param roomUserPermissionDTO the roomUserPermissionDTO to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated roomUserPermissionDTO,
     * or with status {@code 400 (Bad Request)} if the roomUserPermissionDTO is not valid,
     * or with status {@code 404 (Not Found)} if the roomUserPermissionDTO is not found,
     * or with status {@code 500 (Internal Server Error)} if the roomUserPermissionDTO couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PatchMapping(value = "/room-user-permissions/{id}", consumes = { "application/json", "application/merge-patch+json" })
    public ResponseEntity<RoomUserPermissionDTO> partialUpdateRoomUserPermission(
        @PathVariable(value = "id", required = false) final Long id,
        @NotNull @RequestBody RoomUserPermissionDTO roomUserPermissionDTO
    ) throws URISyntaxException {
        log.debug("REST request to partial update RoomUserPermission partially : {}, {}", id, roomUserPermissionDTO);
        if (roomUserPermissionDTO.getId() == null) {
            throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
        }
        if (!Objects.equals(id, roomUserPermissionDTO.getId())) {
            throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
        }

        if (!roomUserPermissionRepository.existsById(id)) {
            throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
        }

        Optional<RoomUserPermissionDTO> result = roomUserPermissionService.partialUpdate(roomUserPermissionDTO);

        return ResponseUtil.wrapOrNotFound(
            result,
            HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, roomUserPermissionDTO.getId().toString())
        );
    }

    /**
     * {@code GET  /room-user-permissions} : get all the roomUserPermissions.
     *
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of roomUserPermissions in body.
     */
    @GetMapping("/room-user-permissions")
    public List<RoomUserPermissionDTO> getAllRoomUserPermissions() {
        log.debug("REST request to get all RoomUserPermissions");
        return roomUserPermissionService.findAll();
    }

    /**
     * {@code GET  /room-user-permissions/:id} : get the "id" roomUserPermission.
     *
     * @param id the id of the roomUserPermissionDTO to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the roomUserPermissionDTO, or with status {@code 404 (Not Found)}.
     */
    @GetMapping("/room-user-permissions/{id}")
    public ResponseEntity<RoomUserPermissionDTO> getRoomUserPermission(@PathVariable Long id) {
        log.debug("REST request to get RoomUserPermission : {}", id);
        Optional<RoomUserPermissionDTO> roomUserPermissionDTO = roomUserPermissionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(roomUserPermissionDTO);
    }

    /**
     * {@code DELETE  /room-user-permissions/:id} : delete the "id" roomUserPermission.
     *
     * @param id the id of the roomUserPermissionDTO to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
    @DeleteMapping("/room-user-permissions/{id}")
    public ResponseEntity<Void> deleteRoomUserPermission(@PathVariable Long id) {
        log.debug("REST request to delete RoomUserPermission : {}", id);
        roomUserPermissionService.delete(id);
        return ResponseEntity
            .noContent()
            .headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
            .build();
    }
}
